package com.quanyan.assets.web;

import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.api.enums.AssetsPayReceive;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.emun.AssetsErrorEnum;
import com.quanyan.assets.emun.StatusType;
import com.quanyan.assets.request.*;
import com.quanyan.assets.response.*;
import com.quanyan.assets.service.EggsHistoryService;
import com.quanyan.assets.service.EggsService;
import com.quanyan.assets.service.UserAccumulateHistoryService;
import com.quanyan.assets.service.UserAssetsService;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.enums.BaseEnum;
import com.quanyan.common.enums.PlatformTypeEnum;
import com.quanyan.common.exception.BizException;
import com.quanyan.orderpay.response.RespPreOrder;
import com.quanyan.orderpay.response.mq.OrderNotifyMsg;
import com.quanyan.orderpay.response.mq.RefundNotifyMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wmb on 2016/6/28.
 */
@RestController
@RequestMapping("/api/assets/eggs")
public class EggsController {
    @Autowired
    private EggsService eggsService;
    @Autowired
    private UserAccumulateHistoryService userAccumulateHistoryService;
    @Autowired
    private EggsHistoryService eggsHistoryService;
    @Autowired
    private UserAssetsService userAssetsService;
    //获取鸟蛋账号
    @RequestMapping(value="/account",method = RequestMethod.POST)
    public APIResponse<RespEggsAccount> account(HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return eggsService.account((int)request.getAttribute("uid"));
    }

    //获取用户鸟蛋历史记录
    @RequestMapping(value="/history",method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespUserAccumulateHistory>>>  history(@RequestBody ReqUserAccumulateHistory reqUserAccumulateHistory,HttpServletRequest request) throws ParseException {
        if(null==reqUserAccumulateHistory){
            reqUserAccumulateHistory=new ReqUserAccumulateHistory();
        }
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        reqUserAccumulateHistory.setUid((int)request.getAttribute("uid"));
//        reqUserAccumulateHistory.setUid(1);
        reqUserAccumulateHistory.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
        List<Byte> statusList=new ArrayList<Byte>();
        statusList.add(StatusType.EFFECTIVE.id.byteValue());
        statusList.add(StatusType.FREEZE.id.byteValue());
        reqUserAccumulateHistory.setStatus(StatusType.EFFECTIVE.id.byteValue()+","+StatusType.FREEZE.id.byteValue());
        APIRequest<ReqUserAccumulateHistory> apiRequest=new APIRequest<ReqUserAccumulateHistory>();
        apiRequest.setData(reqUserAccumulateHistory);
        return userAccumulateHistoryService.getUserAccumulateHistory(apiRequest);
    }

    //获取用户历史详情
    @RequestMapping(value="/historyDetail",method = RequestMethod.POST)
    public APIResponse<RespUserAccumulateHistory> historyDetail(@RequestBody ReqUserAccumulateHistory reqUserAccumulateHistory,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return eggsHistoryService.historyDetailById((int)request.getAttribute("uid"), reqUserAccumulateHistory.getId());
    }

    //充值
    @RequestMapping(value="/rechange",method = RequestMethod.POST)
    public APIResponse<RespPreOrder> rechange(@RequestBody ReqEggsRechange reqEggsRechange,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        if(reqEggsRechange.getOrderAmount().compareTo(new BigDecimal(999999))==1){
            throw new BizException(AssetsErrorEnum.INVALID_ORDER_AMOUNT);
        }
        String userAgent=request.getHeader("User-Agent");
        if(null!=userAgent){
            if(userAgent.indexOf("iPhone")!=-1){
                reqEggsRechange.setPlatformType(PlatformTypeEnum.IOS.getId());
            }else if(userAgent.indexOf("Android")!=-1){
                reqEggsRechange.setPlatformType(PlatformTypeEnum.ANDROID.getId());
            }
        }
        APIRequest<ReqEggsRechange> apiRequest=new APIRequest<ReqEggsRechange>();
        apiRequest.setVer(request.getAttribute("APPVersion")+"");
        apiRequest.setData(reqEggsRechange);
        return eggsService.rechange(apiRequest,(int)request.getAttribute("uid"));
    }

    //掏鸟蛋
    @RequestMapping(value="/randomEggs",method = RequestMethod.POST)
    public APIResponse<RespUserAccumulateModify> randomEggs(@RequestBody ReqRandomEggs reqRandomEggs,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return eggsService.randomEggs(reqRandomEggs,(int)request.getAttribute("uid"));
    }

    //规则使用情况
    @RequestMapping(value="ruleUseStatus",method = RequestMethod.POST)
    public APIResponse<RespRuleUseStatus> ruleUseStatus(@RequestBody ReqRuleUseStatus reqRuleUseStatus,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return eggsService.ruleUseStatus(reqRuleUseStatus,(int)request.getAttribute("uid"));
    }

    //获取当前系统业务规则详情
    @RequestMapping(value="getUserAccumulateAndRule",method = RequestMethod.POST)
    public APIResponse<List<RespRuleDetail>> getUserAccumulateAndRule(@RequestBody ReqRuleDetail reqRuleDetail,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        APIRequest<ReqRuleDetail> apiRequest=new APIRequest<ReqRuleDetail>();
        if(null==reqRuleDetail){
            reqRuleDetail=new ReqRuleDetail();
        }
        reqRuleDetail.setUid((int)request.getAttribute("uid"));
        apiRequest.setData(reqRuleDetail);
        return userAssetsService.getUserAccumulateAndRule(apiRequest);
    }


}
